package com.haiyi.activiti.engine.framework.ruleengine.drools;

import com.google.common.collect.Lists;
import com.haiyi.activiti.engine.model.entity.RuleEngine;
import com.haiyi.activiti.engine.exception.RuleVaildResultException;
import com.haiyi.activiti.engine.framework.ruleengine.RuleEngineEnum;
import com.haiyi.activiti.engine.framework.ruleengine.RuleEngineExecutor;
import com.haiyi.activiti.engine.framework.ruleengine.Stream;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p> </p>
 *
 * <pre> Created: 2018/12/6 17:39 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
@Component
public class DroolsEngineExecutor implements RuleEngineExecutor {
    @Override
    public Stream execute(RuleEngine ruleEngine, Map<String,Object> params) {
        DroolsStream droolsStream = new DroolsStream();
        droolsStream.setParams(params);
        DroolsUtil.executeRule(ruleEngine.getExpression(),droolsStream);
        if(droolsStream.isError()){
            throw new RuleVaildResultException(droolsStream);
        }
        return droolsStream;
    }

    @Override
    public List<String> getServiceTypes() {
        return Lists.newArrayList(RuleEngineEnum.DROOLS.getCode());
    }
}
